package com.ke.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ke.dao.OrderItemRepository;
import com.ke.dao.OrderRepository;
import com.ke.pojo.BaseResp;
import com.ke.pojo.Cart;
import com.ke.pojo.Order;
import com.ke.pojo.OrderItem;
import com.ke.service.AliPayService;
import com.ke.service.OrderService;
import com.ke.utils.AliPayUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.*;

/**
 * @author bochen
 */
@Service
public class AliPayServiceImpl implements AliPayService {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private OrderRepository orderRepository;
    @Resource
    private OrderItemRepository orderItemRepository;
    @Override
    @Transactional(rollbackOn = Exception.class)
    public String pay(List<Integer> list, Integer uid) {
        //从redis中获取到该用户的购物车
        Boolean aBoolean = redisTemplate.hasKey("cart_" + uid);
        Double totalPrice = 0.0;
        OrderItem orderItem=null;
        Date date = new Date();
        final UUID uuid = UUID.randomUUID();
        //获取到该用户的每一个商品
        if (aBoolean&&list.size()>0){
            for (Integer pid:list){
                Object o = redisTemplate.opsForHash().get("cart_" + uid, pid.toString());
                String s = JSONObject.toJSONString(o);
                Cart cart = JSONObject.parseObject(s, Cart.class);
                totalPrice+=cart.getPrice()*cart.getNum();
              //  redisTemplate.opsForHash().delete("cart_"+uid,pid.toString());
                orderItem=new OrderItem(uid,pid,cart.getPrice(),cart.getPrice()*cart.getNum(),cart.getNum(),cart.getPname(),cart.getPimage());
                orderItem.setPayTime(date);
                orderItem.setStatus("Not-Paid");
                orderItem.setOrderUUID(uuid.toString());
                orderItemRepository.saveAndFlush(orderItem);
            }
            //此时将购物车变为订单
            com.ke.pojo.Order order=new Order(date,null,totalPrice,uid,
                    uuid.toString(),"Not-Paid");
            orderRepository.saveAndFlush(order);

            //请求支付宝
            HashMap<String, Object> map1 = new HashMap<>(16);
            map1.put("orderid",uuid.toString());
            map1.put("totalPrice",totalPrice);
            String pay = AliPayUtils.pay(map1);
            return pay;
        }
        return "无商品,错误请求";
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public void updateStatus(String uuid, String status) {
        Order order=orderRepository.findByUuid(uuid);
        List<OrderItem> byUid = orderItemRepository.findByUid(order.getUid());
        if (order!=null){
            if ("TRADE_SUCCESS".equals(status)){
                for (OrderItem orderItem:byUid){
                    orderItem.setStatus("Paid");
                }
                String s=order.getUid().toString();
                redisTemplate.delete("cart_"+s);
            order.setStatus("Paid");
            Date date=new Date();
            order.setPayTime(date);
            orderRepository.saveAndFlush(order);
            }
        }
    }
}
